//
//  STMemorailDetailsController.swift
//  SlowTrajectory
//
//  Created by iOS-Czz on 2023/10/23.
//

import UIKit

class STMemorailDetailsController: STProjectBaseController {

    @IBOutlet var dateLabel: UILabel!
    @IBOutlet var nextLabel: UILabel!
    @IBOutlet var nameTV: UITextView!
    @IBOutlet var wayLabel: UILabel!
    @IBOutlet var remindLabel: UILabel!
    lazy var deleteView: STDeleteTipView = {
        let view = Bundle.main.loadNibNamed("STDeleteTipView", owner: self, options: nil)?.first as! STDeleteTipView
        view.frame = keyWindow!.frame
        return view
    }()
    var model:STMemorailModel?
    lazy var deleteButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "icon_delete"), for: .normal)
        button.addTarget(self, action: #selector(clickDeleteButtonAction), for: .touchUpInside)
        return button
    }()
    lazy var editButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "icon_edit"), for: .normal)
        button.addTarget(self, action: #selector(clickEditButtonAction), for: .touchUpInside)
        return button
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        setNavStyle()
        setPageData()
        // Do any additional setup after loading the view.
    }
    func setPageData(){
        self.nameTV.text = self.model?.name
        self.dateLabel.text = self.model?.date
        self.wayLabel.text =  getWay(self.model!.way)
        let eventDay = model?.date.substring(from:4)
        let next = Date().toString(format: "yyyy") + eventDay!
        self.wayLabel.text = getWay(model!.way)
        let (nextReminderDate, daysUntilReminder) = String.calculateNextReminderDate(from: next, withWay: self.model!.way)
        self.nextLabel.text = nextReminderDate
        self.remindLabel.text = "还剩\(daysUntilReminder)天提醒"
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    func setNavStyle(){
        self.title = "纪念日详情"
        self.navigationItem.rightBarButtonItems = [UIBarButtonItem(customView: self.editButton),UIBarButtonItem(customView: self.deleteButton)]
    }
    
    @objc func clickEditButtonAction(){
        let editVC = STAddAndEditMemorailController()
        editVC.isAdd = false
        editVC.model = self.model
        self.navigationController?.pushViewController(editVC, animated: true)
    }
    @objc func clickDeleteButtonAction(){
        self.deleteView.onSave = {
            self.model?.deleteToDB()
            self.navigationController?.popViewController(animated: true)
        }
        keyWindow!.addSubview(self.deleteView)
    }
    func getWay(_ way:Int) -> String {
        let ways = ["年计算","月计算","周计算"]
        return ways[way]
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
